import utils from '../core/utils';
import { HOST } from '../core/dict';
import logger from '../core/logger';
import { clientBase } from './clientBase';
import { appInfo } from '../models/apis/appInfo';
import ajaxResult from '../models/apis/ajaxResult';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class appClient extends clientBase {
  constructor() {
    super();
  }

  public getApps(pageIndex: number, typeId: number, keywords?: string): Promise<ajaxResult<Array<appInfo>>> {
    return new Promise((resolve) => {
      let url = `${HOST}api/app/getAll?pageIndex=${pageIndex}&typeId=${typeId}&keywords=${utils.isEmpty(keywords) ? '' : keywords}`;
      axios.get<string, AxiosResponse<string>, null>(url).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<appInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<appInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<appInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.error(err);
        resolve(this.buildExceptionResult<Array<appInfo>>(err));
      });
    });
  }

  public getBy(appIds: string): Promise<ajaxResult<Array<appInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/app/getBy?appIds=${appIds}`).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<appInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<appInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<appInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.error(err);
        resolve(this.buildExceptionResult<Array<appInfo>>(err));
      });
    });
  }

  public getUserApps(pageIndex: number, userId: string): Promise<ajaxResult<Array<appInfo>>> {
    return new Promise((resolve) => {
      let url = `${HOST}api/app/getUserApp?pageIndex=${pageIndex}&userId=${userId}`;
      axios.get<string, AxiosResponse<string>, null>(url).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<appInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<appInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<appInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.error(err);
        resolve(this.buildExceptionResult<Array<appInfo>>(err));
      });
    });
  }

  public getAppDetail(appId: string): Promise<ajaxResult<appInfo>> {
    return new Promise((resolve) => {
      let url = `${HOST}api/app/appDetail?appId=${appId}`;
      axios.get<string, AxiosResponse<string>, null>(url)
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: appInfo = response.data.result;
            resolve(this.buildSuccessResult<appInfo>(ret));
          } else
            resolve(this.buildFailedResult<appInfo>(response.statusText));
        }).catch((err: AxiosError) => {
        logger.error(err);
        resolve(this.buildExceptionResult<appInfo>(err));
      });
    });
  }

  public getUpdate(): Promise<ajaxResult<Array<appInfo>>> {
    return new Promise((resolve) => {
      let url = `${HOST}api/app/getNew`;
      axios.get<string, AxiosResponse<string>, null>(url).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<appInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<appInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<appInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.error(err);
        resolve(this.buildExceptionResult<Array<appInfo>>(err));
      });
    });
  }
}
export default new appClient();